#define MyAppName "DrugSupplyErp"
#define MyAppVersion "1.0.6"
#define MyAppPublisher "东莞市茶山新业软件网络科技工作室"
#define MyAppURL ""
#define MyAppExeName "药品供应ERP系统.exe"
#define MyServiceName "drug-supply-erp-service"
#define MySqlPassword "eFaQmRildp"
#define PackageProfile "package-shore"

[Setup]
; 基本信息设置
AppId={{8CD26724-A6EF-4BC3-A280-9C88854C3417}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf}\{#MyAppName}
DefaultGroupName={#MyAppName}
DisableProgramGroupPage=yes
; LicenseFile=EULA.txt
; 如果没有EULA文件，可以注释掉上面一行
OutputDir=output
OutputBaseFilename=drug-supply-erp-setup
Compression=lzma
SolidCompression=yes
PrivilegesRequired=admin
; 禁用安装目录选择界面
DisableDirPage=no
; 添加安装向导的个性化设置
WizardStyle=modern
; WizardImageFile=installer_image.bmp
; WizardSmallImageFile=installer_small_image.bmp
; 如果没有自定义图片，可以注释掉上面两行
SetupIconFile=icon.ico
; 如果没有自定义图标，可以注释掉上面一行

[Languages]
Name: "chinesesimp"; MessagesFile: "compiler:Default.isl,compiler:Languages\ChineseSimplified.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked

[Types]
Name: "full"; Description: "完整安装"
Name: "custom"; Description: "自定义安装"; Flags: iscustom

[Components]
Name: "app"; Description: "客户端程序"; Types: full custom; Flags: fixed
Name: "java"; Description: "Java 11 运行环境"; Types: full custom
Name: "mysql"; Description: "MySQL 8 数据库"; Types: full custom 
Name: "service"; Description: "后端服务"; Types: full custom

[Files]
; Java文件
Source: "jdk\*"; DestDir: "{app}\jdk"; Components: java; Flags: ignoreversion recursesubdirs createallsubdirs
; MySQL文件
Source: "mysql8\*"; DestDir: "{app}\mysql8"; Components: mysql; Flags: ignoreversion recursesubdirs createallsubdirs
; 服务文件
Source: "server\*"; DestDir: "{app}\server"; Components: service; Flags: ignoreversion recursesubdirs createallsubdirs
; 应用程序文件
Source: "app\win-unpacked\*"; DestDir: "{app}\app"; Components: app; Flags: ignoreversion recursesubdirs createallsubdirs
; SQL模板
Source: "drug_supply_erp_template.sql"; DestDir: "{app}"; Components: mysql; Flags: ignoreversion

[Icons]
Name: "{group}\{#MyAppName}"; Filename: "{app}\app\Drug-Supply-ERP.exe"
Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\app\Drug-Supply-ERP.exe"; Tasks: desktopicon

[Run]
; 安装Java环境变量
Filename: "{cmd}"; Parameters: "/c setx JAVA_HOME ""{app}\jdk"" /M"; Components: java; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c setx PATH ""%PATH%;{app}\jdk\bin"" /M"; Components: java; Flags: runhidden

; Visual C++ 环境检查已在 PrepareToInstall 中处理

; 创建MySQL目录结构
Filename: "{cmd}"; Parameters: "/c if not exist ""{app}\mysql8\data"" mkdir ""{app}\mysql8\data"""; Components: mysql; Flags: runhidden

; 先停止可能存在的MySQL服务
Filename: "net"; Parameters: "stop MySQL8"; Components: mysql; Flags: runhidden skipifdoesntexist

; 创建MySQL配置文件
Filename: "{cmd}"; Parameters: "/c echo [mysqld] > ""{app}\mysql8\my.ini"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo basedir={code:GetMySQLBasedir} >> ""{app}\mysql8\my.ini"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo datadir={code:GetMySQLDatadir} >> ""{app}\mysql8\my.ini"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo port=3306 >> ""{app}\mysql8\my.ini"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo character-set-server=utf8mb4 >> ""{app}\mysql8\my.ini"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo default-authentication-plugin=mysql_native_password >> ""{app}\mysql8\my.ini"""; Components: mysql; Flags: runhidden
; 移除bind-address限制，允许从任何IP连接
; Filename: "{cmd}"; Parameters: "/c echo bind-address=0.0.0.0 >> ""{app}\mysql8\my.ini"""; Components: mysql; Flags: runhidden
; Filename: "{cmd}"; Parameters: "/c echo skip-name-resolve >> ""{app}\mysql8\my.ini"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo max_connections=1000 >> ""{app}\mysql8\my.ini"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION' >> ""{app}\mysql8\my.ini"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo [mysql] >> ""{app}\mysql8\my.ini"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo default-character-set=utf8mb4 >> ""{app}\mysql8\my.ini"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo [client] >> ""{app}\mysql8\my.ini"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo port=3306 >> ""{app}\mysql8\my.ini"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo default-character-set=utf8mb4 >> ""{app}\mysql8\my.ini"""; Components: mysql; Flags: runhidden

; 停止并卸载旧的MySQL服务（如果存在）
Filename: "net"; Parameters: "stop MySQL8"; Components: mysql; Flags: runhidden skipifdoesntexist
Filename: "{cmd}"; Parameters: "S/c sc delete MyQL8 >nul 2>&1"; Components: mysql; Flags: runhidden

; 清理旧数据并重新创建数据目录
Filename: "{cmd}"; Parameters: "/c rmdir /S /Q ""{app}\mysql8\data"" >nul 2>&1"; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c mkdir ""{app}\mysql8\data"""; Components: mysql; Flags: runhidden

; 初始化MySQL（无安全模式，root无密码）
Filename: "{app}\mysql8\bin\mysqld.exe"; Parameters: "--initialize-insecure --basedir=""{app}\mysql8"" --datadir=""{app}\mysql8\data"" --console"; Components: mysql; Flags: runhidden

; 等待初始化完成
Filename: "{cmd}"; Parameters: "/c ping -n 5 127.0.0.1 >nul"; Components: mysql; Flags: runhidden

; 安装MySQL服务
Filename: "{app}\mysql8\bin\mysqld.exe"; Parameters: "--install MySQL8 --defaults-file=""{app}\mysql8\my.ini"""; Components: mysql; Flags: runhidden

; 设置MySQL服务为自动启动
Filename: "sc"; Parameters: "config MySQL8 start= auto"; Components: mysql; Flags: runhidden

; 启动MySQL服务
Filename: "net"; Parameters: "start MySQL8"; Components: mysql; Flags: runhidden

; 等待MySQL服务完全启动（加长等待时间）
Filename: "{cmd}"; Parameters: "/c ping -n 20 127.0.0.1 >nul"; Components: mysql; Flags: runhidden

; 使用循环确保MySQL服务完全启动并可连接
Filename: "{cmd}"; Parameters: "/c for /l %%i in (1,1,30) do (""{app}\mysql8\bin\mysql.exe"" -u root --execute=""SELECT 1;"" >nul 2>&1 && goto :ready || ping -n 2 127.0.0.1 >nul) & :ready"; Components: mysql; Flags: runhidden

; 创建MySQL初始化脚本（使用echo命令，但确保UTF-8编码）
Filename: "{cmd}"; Parameters: "/c echo -- MySQL初始化脚本 > ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo -- 创建数据库 >> ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo CREATE DATABASE IF NOT EXISTS drug_supply_erp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; >> ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo -- 使用数据库 >> ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo USE drug_supply_erp; >> ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo -- 导入数据库模板 >> ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo \. {app}\drug_supply_erp_template.sql >> ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo -- 重置root密码 >> ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo ALTER USER 'root'@'localhost' IDENTIFIED BY '{#MySqlPassword}'; >> ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo -- 创建允许任何IP连接的root用户 >> ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo CREATE USER IF NOT EXISTS 'root'@'%%' IDENTIFIED BY '{#MySqlPassword}'; >> ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo -- 授权所有权限 >> ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo GRANT ALL PRIVILEGES ON *.* TO 'root'@'%%' WITH GRANT OPTION; >> ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden
; Filename: "{cmd}"; Parameters: "/c echo -- 创建erp用户 >> ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden
; Filename: "{cmd}"; Parameters: "/c echo CREATE USER IF NOT EXISTS 'erp_admin'@'%%' IDENTIFIED BY '{#MySqlPassword}'; >> ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden
; Filename: "{cmd}"; Parameters: "/c echo -- 授权erp用户 >> ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden
; Filename: "{cmd}"; Parameters: "/c echo GRANT ALL PRIVILEGES ON drug_supply_* TO 'erp_admin'@'%%'; >> ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo -- 刷新权限 >> ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo FLUSH PRIVILEGES; >> ""{app}\mysql_init.sql"""; Components: mysql; Flags: runhidden

; 执行MySQL初始化脚本（使用完整路径和正确的命令格式）
Filename: "{cmd}"; Parameters: "/c echo 执行MySQL初始化脚本... > ""{app}\mysql_init_log.txt"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo 当前时间: %date% %time% >> ""{app}\mysql_init_log.txt"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo MySQL路径: {app}\mysql8\bin\mysql.exe >> ""{app}\mysql_init_log.txt"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo SQL文件路径: {app}\mysql_init.sql >> ""{app}\mysql_init_log.txt"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c cd /d ""{app}\mysql8\bin"" && mysql.exe -u root < ""{app}\mysql_init.sql"" >> ""{app}\mysql_init_log.txt"" 2>&1"; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo MySQL初始化脚本执行完成 >> ""{app}\mysql_init_log.txt"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo 执行完成时间: %date% %time% >> ""{app}\mysql_init_log.txt"""; Components: mysql; Flags: runhidden

; 备用执行方法（如果第一种方法失败）
; Filename: "{cmd}"; Parameters: "/c echo 尝试备用执行方法... >> ""{app}\mysql_init_log.txt"""; Components: mysql; Flags: runhidden
; Filename: "{cmd}"; Parameters: "/c ""{app}\mysql8\bin\mysql.exe"" -u root -e ""source '{app}\mysql_init.sql'"" >> ""{app}\mysql_init_log.txt"" 2>&1"; Components: mysql; Flags: runhidden

; 创建批处理文件来执行MySQL命令（解决编码问题）
Filename: "{cmd}"; Parameters: "/c echo @echo off > ""{app}\mysql_execute.bat"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo chcp 65001 >> ""{app}\mysql_execute.bat"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo cd /d ""{app}\mysql8\bin"" >> ""{app}\mysql_execute.bat"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo mysql.exe -u root ^< ""{app}\mysql_init.sql"" >> ""{app}\mysql_execute.bat"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c ""{app}\mysql_execute.bat"" >> ""{app}\mysql_init_log.txt"" 2>&1"; Components: mysql; Flags: runhidden

; 创建单独的SQL文件来导入模板（解决路径问题）
; Filename: "{cmd}"; Parameters: "/c echo 检查模板文件是否存在... >> ""{app}\mysql_init_log.txt"""; Components: mysql; Flags: runhidden
; Filename: "{cmd}"; Parameters: "/c if exist ""{app}\drug_supply_erp_template.sql"" (echo 模板文件存在 >> ""{app}\mysql_init_log.txt"") else (echo 模板文件不存在 >> ""{app}\mysql_init_log.txt"")"; Components: mysql; Flags: runhidden
; Filename: "{cmd}"; Parameters: "/c echo 模板文件路径: {app}\drug_supply_erp_template.sql >> ""{app}\mysql_init_log.txt"""; Components: mysql; Flags: runhidden
; Filename: "{cmd}"; Parameters: "/c echo -- 导入模板数据 > ""{app}\import_template.sql"""; Components: mysql; Flags: runhidden
; Filename: "{cmd}"; Parameters: "/c echo USE drug_supply_erp; >> ""{app}\import_template.sql"""; Components: mysql; Flags: runhidden
; Filename: "{cmd}"; Parameters: "/c echo \. {app}\drug_supply_erp_template.sql >> ""{app}\import_template.sql"""; Components: mysql; Flags: runhidden
; Filename: "{cmd}"; Parameters: "/c echo 执行模板导入... >> ""{app}\mysql_init_log.txt"""; Components: mysql; Flags: runhidden
; Filename: "{cmd}"; Parameters: "/c cd /d ""{app}\mysql8\bin"" && mysql.exe -u root < ""{app}\import_template.sql"" >> ""{app}\mysql_init_log.txt"" 2>&1"; Components: mysql; Flags: runhidden

; 验证数据库初始化是否成功
; Filename: "{cmd}"; Parameters: "/c echo 验证数据库初始化... > ""{app}\mysql_verify_log.txt"""; Components: mysql; Flags: runhidden
; Filename: "{cmd}"; Parameters: "/c cd /d ""{app}\mysql8\bin"" && mysql.exe -u root -p""{#MySqlPassword}"" --execute=""SHOW DATABASES LIKE 'drug_supply_erp';"" >> ""{app}\mysql_verify_log.txt"" 2>&1"; Components: mysql; Flags: runhidden
; Filename: "{cmd}"; Parameters: "/c cd /d ""{app}\mysql8\bin"" && mysql.exe -u root -p""{#MySqlPassword}"" --execute=""USE drug_supply_erp; SHOW TABLES;"" >> ""{app}\mysql_verify_log.txt"" 2>&1"; Components: mysql; Flags: runhidden

; 清理临时文件
Filename: "{cmd}"; Parameters: "/c del ""{app}\mysql_init.sql"" 2>nul"; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c del ""{app}\mysql_execute.bat"" 2>nul"; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c del ""{app}\import_template.sql"" 2>nul"; Components: mysql; Flags: runhidden

; 创建MySQL安装状态摘要
Filename: "{cmd}"; Parameters: "/c echo MySQL Installation Summary > ""{app}\mysql_installation_summary.txt"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo ========================== >> ""{app}\mysql_installation_summary.txt"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo Installation Date: %date% %time% >> ""{app}\mysql_installation_summary.txt"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo Root Password: {#MySqlPassword} >> ""{app}\mysql_installation_summary.txt"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo ERP Password: {#MySqlPassword} >> ""{app}\mysql_installation_summary.txt"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo Database: drug_supply_erp >> ""{app}\mysql_installation_summary.txt"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo Remote Access: Enabled >> ""{app}\mysql_installation_summary.txt"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo Port: 3306 >> ""{app}\mysql_installation_summary.txt"""; Components: mysql; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo Users: root@localhost, root@%%, erp@%% >> ""{app}\mysql_installation_summary.txt"""; Components: mysql; Flags: runhidden

; 创建服务日志目录
; Filename: "{cmd}"; Parameters: "/c if not exist ""{app}\server\logs"" mkdir ""{app}\server\logs"""; Components: service; Flags: runhidden

; 安全地修改服务配置文件，避免编码问题
; 备份原始配置文件
; Filename: "{cmd}"; Parameters: "/c copy ""{app}\server\drug-supply-erp-service.xml"" ""{app}\server\drug-supply-erp-service.xml.backup"""; Components: service; Flags: runhidden

; 重新创建正确的XML配置文件，使用实际安装的JDK路径
Filename: "{cmd}"; Parameters: "/c echo ^<service^> > ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo   ^<id^>drug-supply-erp-service^</id^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo   ^<name^>DrugManagementERPSystem^</name^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo   ^<description^>YZJ Development Studio Self-developed ERP System^</description^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo   ^<executable^>{app}\jdk\bin\java.exe^</executable^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo   ^<arguments^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-server^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-Xmx2703m^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-Xms2703m^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-Xmn1540m^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-Xss256k^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-XX:MetaspaceSize=256m^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-XX:MaxMetaspaceSize=512m^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-XX:+UseG1GC^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-XX:CICompilerCount=3^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-XX:ParallelGCThreads=4^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-XX:G1ConcRefinementThreads=2^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-XX:ConcGCThreads=2^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-XX:MaxHeapFreeRatio=40^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-XX:InitiatingHeapOccupancyPercent=45^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-XX:MaxDirectMemorySize=256m^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-XX:-ShrinkHeapInSteps^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-XX:+SegmentedCodeCache^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-verbose:gc^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-XX:+PrintCommandLineFlags^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-XX:+ExplicitGCInvokesConcurrent^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-XX:+HeapDumpOnOutOfMemoryError^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-XX:HeapDumpPath=./logs^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-jar^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-Dspring.profiles.active={#PackageProfile}^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>-Dfile.encoding=UTF-8^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo     ^<argument^>drug-supply-erp-service-1.0.6.jar^</argument^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo   ^</arguments^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo   ^<logpath^>./logs^</logpath^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo   ^<logmode^>rotate^</logmode^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo   ^<depend^>MySQL8^</depend^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo   ^<startmode^>Automatic^</startmode^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c echo ^</service^> >> ""{app}\server\drug-supply-erp-service.xml"""; Components: service; Flags: runhidden

; 验证Java路径是否存在
Filename: "{cmd}"; Parameters: "/c echo 验证Java路径... >> ""{app}\service_config_log.txt"""; Components: service; Flags: runhidden
Filename: "{cmd}"; Parameters: "/c if exist ""{app}\jdk\bin\java.exe"" (echo Java路径存在 >> ""{app}\service_config_log.txt"") else (echo Java路径不存在 >> ""{app}\service_config_log.txt"")"; Components: service; Flags: runhidden

; 安装后端服务
Filename: "{app}\server\server_install.bat"; WorkingDir: "{app}\server"; Components: service; Flags: runhidden

; 等待一段时间后启动服务
Filename: "{cmd}"; Parameters: "/c ping -n 5 127.0.0.1 >nul"; Components: service; Flags: runhidden
Filename: "net"; Parameters: "start {#MyServiceName}"; Components: service; Flags: runhidden

; 等待服务启动
Filename: "{cmd}"; Parameters: "/c ping -n 15 127.0.0.1 >nul"; Components: service; Flags: runhidden

; 启动程序并显示安装完成信息
Filename: "{app}\app\Drug-Supply-ERP.exe"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent

[UninstallRun]
; 停止并删除服务
Filename: "net"; Parameters: "stop {#MyServiceName}"; Flags: runhidden
Filename: "{app}\server\server_uninstall.bat"; WorkingDir: "{app}\server"; Flags: runhidden

; 停止并删除MySQL服务
Filename: "net"; Parameters: "stop MySQL8"; Flags: runhidden
Filename: "{app}\mysql8\bin\mysqld.exe"; Parameters: "--remove MySQL8"; Flags: runhidden

[Code]
// 检测Visual C++ Redistributable是否已安装
function IsVCRedistInstalled(): Boolean;
var
  Version: String;
begin
  // 检查注册表中是否存在VC++ 2015-2022 Redistributable (x64)
  Result := RegQueryStringValue(HKLM, 'SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x64', 'Version', Version) or
           RegQueryStringValue(HKLM, 'SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\X64', 'Version', Version) or
           RegQueryStringValue(HKLM, 'SOFTWARE\WOW6432Node\Microsoft\VisualStudio\14.0\VC\Runtimes\x64', 'Version', Version) or
           RegQueryStringValue(HKLM, 'SOFTWARE\WOW6432Node\Microsoft\VisualStudio\14.0\VC\Runtimes\X64', 'Version', Version);
           
  // 如果找不到，也检查旧版本的注册表位置
  if not Result then
  begin
    Result := RegQueryStringValue(HKLM, 'SOFTWARE\Microsoft\DevDiv\VC\Servicing\14.0\RuntimeMinimum', 'Version', Version) or
             RegQueryStringValue(HKLM, 'SOFTWARE\WOW6432Node\Microsoft\DevDiv\VC\Servicing\14.0\RuntimeMinimum', 'Version', Version);
  end;
end;

// 获取MySQL basedir绝对路径
function GetMySQLBasedir(Param: String): String;
begin
  Result := ExpandConstant('"{app}\mysql8"');
end;

// 获取MySQL datadir绝对路径
function GetMySQLDatadir(Param: String): String;
begin
  Result := ExpandConstant('"{app}\mysql8\data"');
end;

// 安装完成后编辑服务配置文件
procedure UpdateServiceConfig();
begin
  if IsComponentSelected('service') then
  begin
    // XML配置文件已在安装过程中重新创建，使用实际安装的JDK路径
    // 配置文件路径: {app}\server\drug-supply-erp-service.xml
    // executable标签已设置为: {app}\jdk\bin\java.exe
  end;
end;

// 准备安装时检查系统依赖
function PrepareToInstall(var NeedsRestart: Boolean): String;
begin
  Result := '';
  NeedsRestart := False;
  
  // 如果选择安装MySQL组件，检查VC++ Redistributable
  if IsComponentSelected('mysql') then
  begin
    if not IsVCRedistInstalled() then
    begin
      MsgBox('系统环境检测失败' + #13#10#13#10 + 
             '检测到您的系统缺少 Microsoft Visual C++ 运行库环境，这可能导致数据库服务无法正常启动。' + #13#10#13#10 + 
             '建议解决方案：' + #13#10 + 
             '1. 下载并运行 DirectXRepair_v4.0' + #13#10 + 
             '2. 使用该工具的"一键修复"功能修复系统运行库' + #13#10 + 
             '3. 修复完成后重新运行本安装程序' + #13#10#13#10 + 
             '下载地址：http://download.yeziji.xyz/' + #13#10#13#10 + 
             '安装程序将自动退出，请修复系统环境后重试。', 
             mbError, MB_OK);
             
      Result := '系统缺少必要的Visual C++运行库环境，安装被终止。请使用DirectX修复工具修复后重试。';
      Exit;
    end;
  end;
end;

// 安装完成后的回调
procedure CurStepChanged(CurStep: TSetupStep);
begin
  // 配置文件修改已在安装过程中完成，无需在此处处理
  if CurStep = ssPostInstall then
  begin
    // 已移除 UpdateServiceConfig() 调用以避免XML编码问题
  end;
end; 